/*============================================================================*/
/*                   Tortoise Team					                          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HAL_UART.c
* version:         1.1
* %created_by:      Oscar Rodea Aragon %
* %date_created:    Sunday Aug 26 07:55:01 2012 %
*=============================================================================*/
/* DESCRIPTION : Source file for uC Abstraction layer for UART                */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the prototypes functions uC Abstraction layer */
/*   for UART module               											  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 26/08/2012 |                               | Oscar Rodea	      */
/*  1.1      | 01/10/2012 |                               | Andres Torres     */
/* Integration under Continuus CM                                             */
/*============================================================================*/
/* Includes */
/* -------- */
#include "HAL_UART.h"
#include "MAL_UART.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 
/* BYTE constants */

//Transmission
#define	ID_TXSpeed				0x31
#define	ID_TXMotors				0x32
#define	ID_TXSteeringWheel		0x33
#define	ID_TXBattery			0x34

#define	ID_TXStripeSensor		0x35
////////////////////////////////////

//Reception
#define	ID_RXMotors				0x36
#define	ID_RXSteeringWheel		0x37

#define	ID_RXMotorsIncrease		0x38
#define	ID_RXMotorsDecrease		0x39
#define	ID_RXMotorsTurnOff		0x61
#define	ID_RXMotorsTurnOn		0x62

#define	ID_TXTesting			0x7A
////////////////////////////////////


/* WORD constants */


/* LONG and STRUCTURE constants */



/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */

//These variables are about reception
T_UBYTE ub_flag = 0;
T_UBYTE ub_id = 0x30;
T_UBYTE ub_data = 0x30;
T_UBYTE ub_stateOfReception = 1;


/* WORD RAM variables */


/* LONG and STRUCTURE RAM variables */


/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */


/* Private functions prototypes */
/* ---------------------------- */



/* Exported functions prototypes */
/* ----------------------------- */



/* Functions */
/* ------------------ */

/**************************************************************
 *  Name                 :	vfnInit_UART
 *  Description          :  Initialization for UART at 9600
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnInit_UART (void)
{	
	vfnInitLINFlex(BR9600);
}

/**************************************************************
 *  Name                 :	vfnSend_Sensors
 *  Description          :  Send the values of sensors
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnSend_Sensors (void)
{	
	T_UBYTE	aub_mask[] = {"AT"};
	
	T_UBYTE ub_centerStripe = 0x31;
	T_UBYTE ub_speed = 0x32;
	T_UBYTE ub_wheel = 0x33;
	T_UBYTE ub_battery = 0x34;
	T_UBYTE ub_PWMmotors = 0x35;
	
	//Read the values//
	/*
	ub_centerStripe = 
	ub_speed = 
	ub_wheel = 
	ub_battery = 
	ub_PWMmotors = 
	*/
	///////////////////
	
	
	/*Center stripe sensor*/	
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXStripeSensor);
	vfnTx_byte(ub_centerStripe);		//Value to send
	
	
	/*Speed*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXSpeed);
	vfnTx_byte(ub_speed);				//Value to send
	
	/*Steering wheel*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXSteeringWheel);
	vfnTx_byte(ub_wheel);				//Value to send
	
	/*Battery*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXBattery);
	vfnTx_byte(ub_battery);				//Value to send
	
	
	/*PWM of motors*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXMotors);
	vfnTx_byte(ub_PWMmotors);			//Value to send
}

/**************************************************************
 *  Name                 :	vfnSend_SensorsAdvance
 *  Description          :  Send the values of advance sensors
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnSend_SensorsAdvance (void)
{	
	T_UBYTE	aub_mask[] = {"AT"};
	
	T_UBYTE ub_centerStripe = 0x31;
	T_UBYTE ub_speed = 0x32;
	T_UBYTE ub_wheel = 0x33;
	T_UBYTE ub_battery = 0x34;
	T_UBYTE ub_PWMmotors = 0x35;
	
	//Read the values//
	/*
	ub_centerStripe = 
	ub_speed = 
	ub_wheel = 
	ub_battery = 
	ub_PWMmotors = 
	*/
	///////////////////
	
	
	/*Center stripe sensor*/
	/*	
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXStripeSensor);
	vfnTx_byte(ub_centerStripe);		//Value to send
	
	*/
	/*Speed*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXSpeed);
	vfnTx_byte(ub_speed);				//Value to send
	
	
	/*Steering wheel*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXSteeringWheel);
	vfnTx_byte(ub_wheel);				//Value to send
	
	/*Battery*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXBattery);
	vfnTx_byte(ub_battery);				//Value to send
	
	/*PWM of motors*/
	/*
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXMotors);
	vfnTx_byte(ub_PWMmotors);			//Value to send
	
	*/
}

/**************************************************************
 *  Name                 :	vfnSend_byte
 *  Description          :  Send a byte
 *  Parameters           :  ub_value The value that will be sent
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnSend_byte (T_UBYTE ub_value)
{	
	T_UBYTE	aub_mask[] = {"AT"};
	
	/*Send something*/
	vfnTx_StringByte(aub_mask);
	vfnTx_byte(ID_TXTesting);
	vfnTx_byte(ub_value);
	
}

/**************************************************************
 *  Name                 :	vfnSend_byte_no_protocol
 *  Description          :  Send a byte without protocol
 *  Parameters           :  ub_value The value that will be sent
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnSend_byte_no_protocol(T_UBYTE ub_value)
{
	vfnTx_byte(ub_value);
}

/**************************************************************
 *  Name                 :	vfnSend_ascii_number
 *  Description          :  Send a number in ascii format through serial interface.
 *  Parameters           :  ub_value The number to send in ascii
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnSend_ascii_number_no_protocol(T_UWORD uw_value)
{
	T_UBYTE ub_divisor = 10;
    T_UBYTE ub_residuo = 0;
    
    do
    {
        ub_residuo = uw_value % ub_divisor;
        uw_value /= ub_divisor;
        
        vfnTx_byte(48 + ub_residuo);
        
    }while(uw_value != 0);
}

/**************************************************************
 *  Name                 :	vfnReceive_DataFromInterrupt
 *  Description          :  Receive a byte
 *  Parameters           :  ub_RxValue, the interrupt will send
 							a new data
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnReceive_DataFromInterrupt (T_UBYTE ub_RxValue)
{	
	T_UBYTE aub_mask[] = {"AT"};
	T_UBYTE ub_newValue = 0;
	
	ub_newValue = ub_RxValue;
	
	switch(ub_stateOfReception)
	{
		case 1:
			if(ub_newValue == aub_mask[0])
			{
				ub_stateOfReception = 2;
			}
			break;
			
		case 2:
			if(ub_newValue == aub_mask[1])
			{
				ub_stateOfReception = 3;
			}
			else
			{
				ub_stateOfReception = 1;
			}
			break;
			
		case 3:
			ub_id = ub_newValue;
			ub_stateOfReception = 4;
			break;
		
		case 4:
			ub_data = ub_newValue;
			ub_stateOfReception = 1;
			ub_flag = 1;
			break;
			
	}
}

/**************************************************************
 *  Name                 :	vfnRx_NewInstruction
 *  Description          :  If a new instruction is available
 							it will execute the commands
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  No
 **************************************************************/
void vfnRx_NewInstruction(void)
{	
	//vfnSend_byte(0x40);
	if(ub_flag)		//New data is available
	{
		ub_flag = 0;
		
		//Put the instruccions to do here
		switch (ub_id)
		{
			case ID_RXMotors:
				vfnSend_byte(0x31);
			break;
			
			case ID_RXSteeringWheel:
				vfnSend_byte(0x32);
			break;
			
			case ID_RXMotorsIncrease:
				vfnSend_byte(0x33);
			break;
			
			case ID_RXMotorsDecrease:
				vfnSend_byte(0x34);
			break;
			
			case ID_RXMotorsTurnOff:
				vfnSend_byte(0x35);
			break;
			
			case ID_RXMotorsTurnOn:
				vfnSend_byte(0x36);
			break;
		}
		
		
		//Testing
		//vfnSend_byte(ub_id);
		//vfnSend_byte(ub_data);
	}
}

